import { _decorator, Component, instantiate, Label, Layout, macro, Node, PageView, ProgressBar, ScrollView, Slider, Sprite, Toggle, Vec2, Vec3 } from 'cc';
import { UIManager } from '../UIManager';
import { LanauageManager } from '../LanauageManager';
import { GameData, shopCfg, shopData } from '../GameData';
import { HttpClient } from '../net/HttpClient';
import { EventManager } from '../EventManager';
import { AudioMgr } from '../AudioMgr';
import { Util } from '../Util';
const { ccclass, property } = _decorator;

@ccclass('popupShopMang')
export class popupShopMang extends Component {

    @property(Node)
    showNode:Node = null;

    @property(PageView)
    shopView:PageView = null;

    @property(Label)
    nameLab:Label = null;

    @property(Node)
    getBtn:Node = null;

    @property(Label)
    btnLab:Label = null;

    @property(Node)
    coinBg:Node = null;

    @property(Label)
    coinLab:Label = null;

    @property(Node)
    ohterNode:Node = null;

    @property(ProgressBar)
    progress:ProgressBar = null;

    @property(Label)
    timeLab:Label = null;

    @property(Label)
    desLab:Label = null;

    @property(Node)
    emptyNode:Node = null;

    /**1管理 补货， 2升级 */
    type:number = 1;

    selectId:number = 5;

    selectCfg:shopCfg;

    selectData:shopData;

    showList:shopData[]

    curTime:number; //倒计时

    /**0 营业中  1 待补货 2 补货中 3 升级中  4 停业中 */
    statu:number;

    protected start(): void {
        EventManager.eventTarget.on(EventManager.REFRESH_GAME, this.refresh, this);
    }

    updateShowList(){
        HttpClient.getInstance().sendGetShopList()
    }
    
    onEnable() {
        this.updateShowList();

        this.type = UIManager.openData.type;

        this.refresh();
    }

    refresh(){
        if (!this.node.active){
            return;
        }
        this.statu = 0;

        this.emptyNode.active = false;
        this.showNode.active = false;
        this.ohterNode.active = false;

        this.selectCfg = GameData.getItemDataByIdAndCfgs(this.selectId, GameData.shopCfgs);
        if (!this.selectCfg){
            return;
        }

        this.showList = [];

        for (let index = 0; index < GameData.shopDataList.length; index++) {
            let itemData:shopData = GameData.shopDataList[index];
            if (itemData.lv == this.selectId){
                this.showList.push(itemData);
                if (this.type == 1 && itemData.status == 2){
                    this.statu = 2;
                    this.selectData = itemData;
                    break;
                }else if (this.type == 2 && itemData.status == 3){
                    this.statu = 3;
                    this.selectData = itemData;
                    break;
                }
            }
        }
        this.removeSche();
        if (this.showList.length == 0){
            this.emptyNode.active = true;
        }
        else if (this.statu == 2){
            this.ohterNode.active = true;
            this.desLab.string = "店铺补货中";
            this.progress.progress = (this.selectCfg.addGoodsTime - this.selectData.time)/this.selectCfg.addGoodsTime;
            this.curTime = this.selectData.time;

            this.schedule(this.updateTimeLab, 1, macro.REPEAT_FOREVER, 0.1);
        }
        else if (this.statu == 3){
            this.ohterNode.active = true;
            this.desLab.string = "店铺升级中";

            this.progress.progress = (this.selectCfg.addGoodsTime - this.selectData.time)/this.selectCfg.upGradeTime;
            this.curTime = this.selectData.time;


            this.schedule(this.updateTimeLab, 1, macro.REPEAT_FOREVER, 0.1);
        }
        else{
            this.showNode.active = true;
            this.nameLab.string = this.selectCfg.name;

            // 刷新商店
            for (let index = 0; index < this.shopView.content.children.length; index++) {
                const element = this.shopView.content.children[index];
                element.active = false;
            }

            for (let index = 0; index < this.showList.length; index++) {
                let itemData:shopData = this.showList[index];
                let item = this.shopView.content.children[index];
                if (!item){
                    item = instantiate(this.shopView.content.children[0])
                    this.shopView.addPage(item);
                }
                item.active = true;

                Util.loadSprite("imag/shop/" + itemData.lv, item.getChildByName("icon").getComponent(Sprite));
            }

            this.onPageViewChange()
        }
    }

    updateTimeLab(){ 
        if (this.statu == 2 || this.statu == 3){
            this.curTime--
            this.timeLab.string = "倒计时：" + this.curTime;
            if (this.curTime <= 0){
                this.updateShowList();
                this.statu = 0
                this.removeSche();
            }
        }
    }

    removeSche(){
        this.unschedule(this.updateTimeLab);
    }

    onClickHandler(event:Event, str:string){
        if (str == "getBtn"){
            if (this.type == 1){
                if (!Util.isEnoughCoin(this.selectCfg.stockCoin)){
                    return;
                }
                let curindex = this.shopView.getCurrentPageIndex();
                if (this.showList[curindex]){
                    HttpClient.getInstance().sendAddGoods(this.showList[curindex].id, ()=>{
                        this.updateShowList();
                    });
                }
            }else if (this.type == 2){
                if (this.selectId == 5){
                    UIManager.showTip("已达到最高级")
                    return;
                }
                if (!Util.isEnoughCoin(this.selectCfg.lvCoin)){
                    return;
                }
                let curindex = this.shopView.getCurrentPageIndex();
                if (this.showList[curindex]){
                    HttpClient.getInstance().sendUpShop(this.showList[curindex].lv, this.showList[curindex].id, ()=>{
                        this.updateShowList();
                    });
                }
            }
            return;
        }
        else if (str == "tog1"){
            this.selectId = 1;
        }
        else if (str == "tog2"){
            this.selectId = 2;
        }
        else if (str == "tog3"){
            this.selectId = 3;
        }
        else if (str == "tog4"){
            this.selectId = 4;
        }
        else if (str == "tog5"){
            this.selectId = 5;
        }

        this.refresh();
    }

    public onPageViewChange(){
        let curindex = this.shopView.getCurrentPageIndex();
        this.selectData = this.showList[curindex]
         if (this.type == 1){
            // if (this.selectData.status == 1){
                this.getBtn.active = true;
                this.coinBg.active = true;
                this.coinLab.string = this.selectCfg.stockCoin + "";
                this.btnLab.string = "补货";
            // }else{
            //      this.getBtn.active = false;
            //     this.coinBg.active = false;
            // }
        }else if (this.type == 2){
            if (this.selectData.lv != 5){
                this.coinLab.string = this.selectCfg.lvCoin + "";
                this.btnLab.string = "升级店铺";
                this.getBtn.active = true;
                this.coinBg.active = true;
            }else{
                this.getBtn.active = false;
                this.coinBg.active = false;
            }
        }
    }

    protected onDisable(): void {
        this.removeSche();
    }
}

